package cn.tuling.org.controller;

import cn.tuling.basic.util.AjaxResult;
import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Writer;
import cn.tuling.org.query.WriterQuery;
import cn.tuling.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/writer")
public class WriterController {
    @Autowired
    private WriterService writerService;

    @PostMapping
    public PageList<Writer> queryPage(@RequestBody WriterQuery query){
        return writerService.queryPage(query);
    }

    // 删除AjaxResult
    @DeleteMapping("/{id}")
    public AjaxResult delWriterById(@PathVariable("id") Long id){
        try {
            writerService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    // 新增和编辑
    @PutMapping
    public AjaxResult addAndUpd(@RequestBody Writer writer){
        try {
            if (writer.getId()==null){
                writerService.add(writer);
            } else {
                writerService.update(writer);
                System.out.println(writer);
            }
            return AjaxResult.me().success();
        } catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
}
